package com.hqyj.lyf.utils;

/**
 * @author Qiuluoer
 * @date 2022/6/1 16:27
 */
import cn.hutool.poi.excel.ExcelUtil;
import com.hqyj.lyf.pojo.QuestionBank;

import java.io.InputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtils {

    public static List<QuestionBank> praseExcel(InputStream inputStream){
        List<QuestionBank> list1 = new ArrayList<>();
        ExcelUtil.readBySax(inputStream, 0, (sheetIndex, rowIndex, list) -> {
            //System.out.println(sheetIndex); // 输出第几张表格 从0开始
            System.out.println(rowIndex); // 输出第几行数据 从0开始 第一行一般为表头
            System.out.println(list); // 每行的数据 封装在一个列表里面

            if(rowIndex > 0) {
                QuestionBank questionBank = new QuestionBank();
                for (int i = 0; i < list.size(); i++) {
                    switch (i) {
                        case 1:
                            questionBank.setQuContent((String) list.get(1));
                            break;
                        case 2:
                            Object o = list.get(2);
                            if(o instanceof Long){
                                questionBank.setQuAoption(o.toString());
                            }else {
                            questionBank.setQuAoption((String) list.get(2));
                            }
                            break;
                        case 3:
                            Object o2 = list.get(3);
                            if(o2 instanceof Long){
                                questionBank.setQuBoption(o2.toString());
                            }else {
                                questionBank.setQuBoption((String) list.get(3));
                            }
                            break;
                        case 4:
                            Object o3 = list.get(4);
                            if(o3 instanceof Long){
                                questionBank.setQuCoption(o3.toString());
                            }else {
                                questionBank.setQuCoption((String) list.get(4));
                            }
                            break;
                        case 5:
                            Object o4 = list.get(5);
                            if(o4 instanceof Long){
                                questionBank.setQuDoption(o4.toString());
                            }else {
                                questionBank.setQuDoption((String) list.get(5));
                            }
                            break;
                        case 6:
                            questionBank.setQuType((String) list.get(6));
                            break;
                        case 7:
                            questionBank.setQuAnswer((String) list.get(7));
                            break;
                        case 8:
                            Long l = (Long) list.get(8);
                            questionBank.setQuScore(l.intValue());

                            break;
                        case 9:
                            questionBank.setSubName((String) list.get(9));
                            break;
                    }
                }
                questionBank.setUpDateTime(new Timestamp(System.currentTimeMillis()));
                list1.add(questionBank);
            }

        });
        list1.forEach(questionBank -> System.out.println(questionBank.toString()));
        return list1;
    }

}